package com.frejoys.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.AdminAddUserCashDto;
import com.frejoys.common.dto.app.AddUserCashDto;
import com.frejoys.common.dto.admin.UpUserCashDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.UserCashListVo;
import com.frejoys.service.UserCashService;
import com.frejoys.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "用户提现账号管理", description = "用户提现账号管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminUserCashController {

    private final UserCashService userCashService;

    private final RedissonClient redissonClient;

    private final UserService userService;

    @PostMapping("/userCash/userCashListInfo")
    @Operation(summary = "提现账号列表")
    public Result<IPage<UserCashListVo>> userCashListInfo(@RequestBody @Validated PageDto dto){
        return Result.success(userCashService.userCashListInfo(dto));
    }

    @PostMapping("/userCash/upUserCash")
    @Operation(summary = "编辑提现信息")
    public Result upUserCash(@RequestBody @Validated UpUserCashDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.upUserCashLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                userCashService.upUserCash(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }


    @PostMapping("/userCash/addUserCash")
    @Operation(summary = "添加提现信息")
    public Result addUserCash(@RequestBody @Validated AdminAddUserCashDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.userCashLockCacheKey(RequestUtil.userId()));
        AddUserCashDto addUserCashDto = BeanUtil.copyProperties(dto, AddUserCashDto.class);
        Long userId = userService.phoneByUserId(dto.getPhone());

        if (lock.tryLock()) {
            try {
                userCashService.addUserCash(addUserCashDto,userId);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }
}
